package com.woman.beautylive.presentation.ui.main.find;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.woman.beautylive.R;
import com.woman.beautylive.data.bean.AnchorSummary;
import com.woman.beautylive.data.repository.SourceFactory;
import com.woman.beautylive.presentation.ui.base.BaseActivity;
import com.woman.beautylive.presentation.ui.base.recycler.SimpleRecyclerAdapter;
import com.woman.beautylive.presentation.ui.base.recycler.SimpleRecyclerHolder;
import com.woman.beautylive.presentation.ui.main.find.custom.PagerSlidingTabStrip;
import com.woman.beautylive.util.PicUtil;

import java.util.List;

/**
 * Created by admin on 2016/9/12.
 */
public class PaiHangBangActivity extends BaseActivity implements ViewPager.OnPageChangeListener {
    String[] titles = { "魅力榜", "富豪榜" };
    TextView txt_meilibang, txt_fuhaobang;
    ViewPager pager;
    FuHaoBangFragment fuHaoBangFragment;
    MeiLiBangFragment meiLiBangFragmentl;
//    ArrayList<Fragment> fragmentArrayList=new ArrayList<>();
    PagerSlidingTabStrip title;
    ImageView back;
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_billboard;
    }

    @Override
    protected void findViews(Bundle savedInstanceState) {
        pager = (ViewPager) findViewById(R.id.pager);
        title = (PagerSlidingTabStrip) findViewById(R.id.title);
        pager.setAdapter(new MyAdapter(getSupportFragmentManager(), titles));
        title.setViewPager(pager);
        back= (ImageView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });
    }
    public class MyAdapter extends FragmentStatePagerAdapter {
        String[] _titles;

        public MyAdapter(FragmentManager fm, String[] titles) {
            super(fm);
            _titles = titles;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return _titles[position];
        }

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public Fragment getItem(int position) {
            Fragment bfrag2 =new MeiLiBangFragment();
            Bundle bundle=new Bundle();
            bundle.putInt("type",position);
            bfrag2.setArguments(bundle);
            return bfrag2;
        }
    }

    @Override
    protected void init() {

//        fuHaoBangFragment=FuHaoBangFragment.newInstance();
//        meiLiBangFragmentl=MeiLiBangFragment.newInstance();

    }

    public static Intent createIntent(Context context) {
        return new Intent(context, PaiHangBangActivity.class);
    }



    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        switch (position){
            case 0:
                break;
            case 1:
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    private class PaiHangBangAdapter extends SimpleRecyclerAdapter<AnchorSummary,
            CommendHolder> {
        public PaiHangBangAdapter(List<AnchorSummary> anchorSummaries) {
            super(anchorSummaries);
        }

        @Override
        protected int getItemLayoutId(int viewType) {
            return R.layout.item_recommend_user_result;
        }

        @NonNull
        @Override
        protected CommendHolder createHolder(View view) {
            return new CommendHolder(view);
        }
    }

    private class CommendHolder extends SimpleRecyclerHolder<AnchorSummary> {

        private TextView tvName;
        private SimpleDraweeView draweeAvatar;
        private ImageView imgLevel, imgStar;


        public CommendHolder(View itemView) {
            super(itemView);
            tvName = (TextView) itemView.findViewById(R.id.item_search_anchor_tv_city);
//            tvIntro = (TextView) itemView.findViewById(R.id.item_search_anchor_tv_intro);
            draweeAvatar = (SimpleDraweeView) itemView.findViewById(R.id.img_user_avatar);
//            imgGender = (ImageView) itemView.findViewById(R.id.item_search_anchor_img_gender);
            imgLevel = (ImageView) itemView.findViewById(R.id.item_search_anchor_img_level);
            imgStar = (ImageView) itemView.findViewById(R.id.img_user_star_type);
//            imgbtnFollow = (ImageButton) itemView.findViewById(R.id.item_search_anchor_imgbtn_follow);
        }

        @Override
        public void displayData(final AnchorSummary data) {
//            imgbtnFollow.setVisibility(View.GONE);

            tvName.setText(data.getNickname());
//            tvIntro.setText(data.getIntro());
            if (!TextUtils.isEmpty(data.getAvatar())) {
                draweeAvatar.setImageURI(SourceFactory.wrapPathToUri(data.getAvatar()));
            }

            imgLevel.setImageResource(PicUtil.getLevelImageId(itemView.getContext(), data.getEmceeLevel()));

        }
    }
}
